package com.wxlgroup.springboot.service.serviceImp;
import com.wxlgroup.springboot.commons.Response;
import com.wxlgroup.springboot.dao.DepartmentDao;
import com.wxlgroup.springboot.dao.UserDao;
import com.wxlgroup.springboot.entity.User;
import com.wxlgroup.springboot.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @auther:wxl
 * @Date:2022/5/20-05-20-19:11
 * @Description:com.wxlgroup.springboot.service.serviceImp
 * @Version:1.0
 */
@Service
public class UserServiceImp implements UserService {

    @Resource
    private UserDao userDao;

    @Resource
    private DepartmentDao departmentDao;

    @Override
    public Response queryAll() {
        List<User> users = userDao.queryAll();
        if(users!=null){
            return new Response("200",users,"查询成功");
        }else {
            return new Response("500",null,"查询失败");
        }
    }

    @Override
    public Response save(User user) {
        Integer integer=0;
        Response response = new Response();
        if (user.getId()==null){        //没有id说明是新增
            try {
                Integer departmentId = departmentDao.queryDepartmentIdByName(user.getDepartmentName());
                user.setDepartmentId(departmentId);
                integer = userDao.insertUser(user);
            }catch (Exception e){
                response.setStateCode("500");
                response.setMessage("用户名重复");
                return response;
            }
            if(integer>0){
                return new Response("200",null,"插入成功");
            }else {
                return new Response("500",null,"插入失败");
            }
        }else {     //否则则为修改用户
            Integer departmentId = departmentDao.queryDepartmentIdByName(user.getDepartmentName());
            user.setDepartmentId(departmentId);
            integer = userDao.updateUserById(user);
            if(integer>0){
                return new Response("200",user,"修改成功");
            }else {
                return new Response("500",null,"修改失败");
            }
        }


    }

    @Override
    public Response deleteUserById(Integer id) {
        Integer integer = userDao.deleteUserById(id);
        if(integer>0){
            return new Response("200",null,"删除成功");
        }else {
            return new Response("500",null,"删除失败");
        }
    }

    @Override
    public Response queryUserById(Integer id) {
        User user = userDao.queryUserById(id);
        if(user!=null){
            return new Response("200",user,"查询成功");
        }else {
            return new Response("500",null,"没有此用户");
        }
    }

    @Override
    public Response deleteUsers(List<Integer> ids) {
        boolean flag=true;
        for (Integer id : ids) {
            if(userDao.deleteUserById(id)<1){
                flag=false;
            }
        }
        if(flag){
            return new Response("200",null,"批量删除成功");
        }else {
            return new Response("500",null,"批量删除失败");
        }
    }

    @Override
    public Integer findUserCounts(String username,String email,String address) {
        return userDao.findUserCounts(username,email,address);
    }

    @Override
    public Response findUsersByPage(Integer pageNumber, Integer PageSize,String username,String email,String address) {
        Integer start=(pageNumber-1)*PageSize;
        Integer end=PageSize;
        if(!"".equals(username)){
            username="%"+username+"%";
        }else {
            username="%%";
        }
        if(!"".equals(email)){
            email="%"+email+"%";
        }else {
            email="%%";
        }
        if(!"".equals(address)){
            address="%"+address+"%";
        }else {
            address="%%";
        }
        List<User> usersByPage = userDao.findUsersByPage(start, end,username,email,address);
        Integer totalCounts = userDao.findUserCounts(username,email,address);
        Map<String,Object> data = new HashMap<>();
        data.put("data",usersByPage);
        data.put("totalCounts",totalCounts);
        Response response =new Response();
        if(usersByPage!=null){
            response.setMessage("分页查询成功");
            response.setStateCode("200");
            response.setResponseData(data);
        }else {
            response.setMessage("分页查询失败");
            response.setStateCode("500");
            response.setResponseData(null);
        }
        return response;
    }

    @Override
    public List<User> exportUsers() {
        return userDao.queryAll();
    }

    @Override
    public Response findUserByUsername(String username) {
        User userByUsername = userDao.findUserByUsername(username);
        return new Response("200",userByUsername,"查询成功");
    }
}
